#!/bin/sh

create_mock_table(){
  local NAME="${1:-$CLUSTER_NAME}"
  local INSTANCE="${2:-0}"
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -q "CREATE DATABASE test;"
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -d "test" -q "CREATE TABLE fibonacci(num integer);"
}

insert_mock_data(){
  local NAME="${1:-$CLUSTER_NAME}"
  local INSTANCE="${2:-0}"
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -d "test" -q 'TRUNCATE TABLE fibonacci;'
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -d "test" -q 'INSERT INTO fibonacci(num) VALUES (0),(1),(1);'
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -d "test" -q 'INSERT INTO fibonacci(num) VALUES (2),(3),(5);'
  run_query -c "$NAME" -p 5432 -i "$INSTANCE" -d "test" -q 'INSERT INTO fibonacci(num) VALUES (8),(13),(21);'
}

generate_mock_data(){
  local NAME="${1:-$CLUSTER_NAME}"
  create_mock_table "$NAME"
  insert_mock_data "$NAME"
}

read_mock_table(){
  local SERVER="$([ -z "$SERVER" ] || printf %s "-$SERVER")"
  local INSTANCE="${INSTANCE:-1}"

  local opt
  local OPTARG
  local OPTIND

  while getopts ":s:i:" opt; do
    case $opt in
      s) SERVER="$OPTARG"
      ;;
      i) INSTANCE="$OPTARG"
      ;;
      \?) echo "Invalid option -$OPTARG" >&2
      return 1
      ;;
    esac
  done

  run_query -c "$CLUSTER_NAME" -p 5432 -h "${CLUSTER_HOST:-$CLUSTER_NAME$SERVER}" -i "$INSTANCE" -d "test" -q "SELECT SUM(num) FROM fibonacci;"
}

check_mock_data_samehost(){
  local CLUSTER_NAME="${1:-$CLUSTER_NAME}"
  local INSTANCE="${2:-0}"
  local RESPONSE
  RESPONSE="$(read_mock_table -i "$INSTANCE")"
  if [ "$RESPONSE" = "54" ]
  then
    echo "Mock data is present"
  else
    return 1
  fi
}

check_mock_data(){
  local CLUSTER_NAME="${1:-$CLUSTER_NAME}"
  local INSTANCE="${2:-1}"
  local RESPONSE
  RESPONSE="$(read_mock_table "$INSTANCE")"
  if [ "$RESPONSE" = "54" ]
  then
    echo "Mock data is present"
  else
    return 1
  fi
}

check_mock_data_replication(){
  local CLUSTER_NAME="${1:-$CLUSTER_NAME}"
  local REPLICA_INDEX
  local SERVER
  local RESPONSE_PRIMARY
  REPLICA_INDEX="$(get_replica_index)"
  SERVER=""
  RESPONSE_PRIMARY="$(read_mock_table -i "$REPLICA_INDEX")"

  local PRIMARY_INDEX
  local SERVER
  local RESPONSE_PRIMARY
  PRIMARY_INDEX="$(get_primary_index)"
  SERVER="replicas"
  RESPONSE_REPlICA="$(read_mock_table -i "$PRIMARY_INDEX")"
  if [ "$RESPONSE_PRIMARY" = "$RESPONSE_REPlICA" ]
  then
    return 0
  else
    echo "FAIL. data not replication"
    return 1
  fi
}

get_replica_index() {
  kubectl -n "$CLUSTER_NAMESPACE" get pod -l role=replica | tail -n 1 | awk '{print $1}' | awk -F "-" '{print $NF}'
}

get_primary_index() {
  PATRONI_MAJOR_VERSION="$(kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" -c patroni -- \
    patronictl version 2>/dev/null | sed -n 's/^patronictl version \([0-9]\+\)\..*$/\1/p')"
  if [ "$PATRONI_MAJOR_VERSION" -lt 4 ]
  then
    kubectl -n "$CLUSTER_NAMESPACE" get pod -l role=master | tail -n 1 | awk '{print $1}' | awk -F "-" '{print $NF}'
  else
    kubectl -n "$CLUSTER_NAMESPACE" get pod -l role=primary | tail -n 1 | awk '{print $1}' | awk -F "-" '{print $NF}'
  fi
}
